package com.hb.software.controller.holiday;

import com.hb.software.controller.BaseController;
import com.hb.software.domain.Result;
import com.hb.software.entity.holiday.HolidayMessage;
import com.hb.software.holiday.HolidayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("holiday")

public class HolidayController extends BaseController<HolidayMessage,Long, HolidayService> {
    private static final Logger logger = LogManager.getLogger(HolidayController.class.getName());

    private HolidayService holidayService;

    @Autowired
    public void setHolidayService(HolidayService holidayService) {
        this.holidayService = holidayService;
        this.manager = this.holidayService;
    }
    @PostMapping("/findLastId")
    public Result findLastId()   {
        try{
            long id = holidayService.findLastId();
            return Result.success(id);
        }catch (Exception e){
            return Result.success(0);
        }

    }
    @PostMapping("/findByName")
    public Result findByName(@RequestBody String name) throws UnsupportedEncodingException {
        //处理中文乱码问题
        name = URLDecoder.decode(name,"UTF-8");
        name = name.substring(0,name.length()-1);
        logger.info(name);
        List list=holidayService.findByName(name);
        return Result.success(list);
    }
    @PostMapping("/findByDepartment")
    public Result findByDepartment(@RequestBody String department) throws UnsupportedEncodingException {
        //处理中文乱码问题
        department = URLDecoder.decode(department,"UTF-8");
        department = department.substring(0,department.length()-1);
        logger.info(department);
        List list=holidayService.findByDepartment(department);
        return Result.success(list);
    }
    @PostMapping("/exportExcel")
    public Result exportExcel(@RequestBody String path)throws UnsupportedEncodingException {
        //处理中文乱码问题
        path = URLDecoder.decode(path, "UTF-8");
        path = path.substring(0, path.length() - 1);
        List list = holidayService.findAll();

        String str = holidayService.createExcel(list, path);
        if (str.equals("success"))
            return Result.success();
        else return Result.error("路径错误");
    }
    @PostMapping("/findByStates")
    public Result findByStates(@RequestBody String states) throws UnsupportedEncodingException {
        //处理中文乱码问题
        states = URLDecoder.decode(states,"UTF-8");
        states = states.substring(0,states.length()-1);
        logger.info(states);
        List list=holidayService.findByStates(states);
        return Result.success(list);
    }
    @PostMapping("/save")
    public Result save(@RequestBody HolidayMessage holiday ){
        try {
            holiday.setId(holidayService.findLastId()+1);
        }catch (Exception e) {
            holiday.setId(1L);
        }
        manager.save(holiday);
        return Result.success();
    }

}
